// 导入bcrypt
const bcrypt = require('bcryptjs');
const uuid = require('uuid');
const path = require('path')

const {User,findUserByAccount} = require('../../models/User');
const {isTelCode} = require('../../plugins/utils/check');

module.exports = async (req,res) => {
	const {email,phoneNumber,password,nickname} = req.body;
	// 根据邮箱地址查询用户是否存在
	let user = await User.findOne({email: email});
	if(user){
		res.status(400).send({errCode:'1',msg:"邮箱已注册"});
		return false;
	}
	let user2 = await User.findOne({phoneNumber: phoneNumber});
	if(user2){
		res.status(400).send({errCode:'1',msg:"手机号已注册"});
		return false;
	}
	let user3 = await User.findOne({nickname: nickname});
	if(user3){
		res.status.send({errCode:'1',msg:'昵称已存在'});
		return false;
	}
	//创建用户
	const salt = bcrypt.genSaltSync(10);
	const pass = bcrypt.hashSync(password, salt);
    const avatarDir = "/uploads/images/avatar/portrait.jpg";
    const user_id = uuid.v1();
	const newuser = await User.create({
        user_id: user_id,
	    nickname: nickname,
        phoneNumber: phoneNumber,
		email: email,
		password: pass,
        sex: "保密",
        avatar: avatarDir,
        address: '',
        signature:'这个人什么都没留下...',
        receivingAddress: [],
		"summary":"欢迎光临",
		"follows":[],
		"fans":[],
		"collections":[],
	});
	console.log("用户注册成功!");
	res.send({errCode:'0',msg:"注册成功"})
}